package disco;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Date;
import java.util.concurrent.SynchronousQueue;
import proceso.DispatchPeticiones;
import proceso.XMLParser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import tiempo.Temporizador;

public class Main extends javax.swing.JFrame{

    public Main() {
        initComponents();
        dispatchQueueAreaGui.setEditable(false);
        peticionesDiscoAreaGui.setEditable(false);
        peticionesProcesosAreaGui.setEditable(false);
        resumenAreaGui.setEditable(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        peticionesProcesosGui = new javax.swing.JScrollPane();
        peticionesProcesosAreaGui = new javax.swing.JTextArea();
        peticionesDiscoGui = new javax.swing.JScrollPane();
        peticionesDiscoAreaGui = new javax.swing.JTextArea();
        dispatchQueueGui = new javax.swing.JScrollPane();
        dispatchQueueAreaGui = new javax.swing.JTextArea();
        resumenGui = new javax.swing.JScrollPane(resumenAreaGui);
        resumenAreaGui = new javax.swing.JTextArea();
        btnNext = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        peticionesProcesosAreaGui.setColumns(20);
        peticionesProcesosAreaGui.setRows(5);
        peticionesProcesosGui.setViewportView(peticionesProcesosAreaGui);

        peticionesDiscoAreaGui.setColumns(20);
        peticionesDiscoAreaGui.setRows(5);
        peticionesDiscoGui.setViewportView(peticionesDiscoAreaGui);

        dispatchQueueAreaGui.setColumns(20);
        dispatchQueueAreaGui.setRows(5);
        dispatchQueueGui.setViewportView(dispatchQueueAreaGui);

        resumenAreaGui.setColumns(20);
        resumenAreaGui.setRows(5);
        resumenGui.setViewportView(resumenAreaGui);

        btnNext.setText("Saltar al final de la simulación");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        jLabel1.setText("Peticiones realizadas por los procesos");

        jLabel3.setText("Dispatch Queue");

        jLabel4.setText("Peticiones realizadas al disco: CFQRequests");

        jLabel5.setText("Resumen procesos");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(peticionesProcesosGui, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(dispatchQueueGui, javax.swing.GroupLayout.PREFERRED_SIZE, 614, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(298, 298, 298)
                                .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(peticionesDiscoGui, javax.swing.GroupLayout.PREFERRED_SIZE, 620, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(resumenGui, javax.swing.GroupLayout.PREFERRED_SIZE, 620, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {dispatchQueueGui, peticionesDiscoGui, peticionesProcesosGui, resumenGui});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dispatchQueueGui, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                    .addComponent(peticionesProcesosGui, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(peticionesDiscoGui, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(resumenGui, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {dispatchQueueGui, peticionesDiscoGui, peticionesProcesosGui, resumenGui});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        finalizarSimulacion = true;
        Temporizador.tiempoSleep = 0;

    }//GEN-LAST:event_btnNextActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNext;
    private static javax.swing.JTextArea dispatchQueueAreaGui;
    private javax.swing.JScrollPane dispatchQueueGui;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private static javax.swing.JTextArea peticionesDiscoAreaGui;
    private javax.swing.JScrollPane peticionesDiscoGui;
    private static javax.swing.JTextArea peticionesProcesosAreaGui;
    private javax.swing.JScrollPane peticionesProcesosGui;
    private static javax.swing.JTextArea resumenAreaGui;
    private javax.swing.JScrollPane resumenGui;
    // End of variables declaration//GEN-END:variables

    static final Date fecha = new Date();
    static protected String newline = "\n";
    static long tiempoComienzoApp = fecha.getTime();
    static boolean finalizarSimulacion = false;
    static DispatchQueue dispatchQueue = new DispatchQueue();
    public static int cabezal = 0;

    public static void actualizarPeticionesProcesosGui(String toAppend){
        peticionesProcesosAreaGui.append(toAppend + newline);
        peticionesProcesosAreaGui.setCaretPosition(peticionesProcesosAreaGui.getDocument().getLength());
    }

    public static void actualizarDispatchQueueGui(String toAppend){
        dispatchQueueAreaGui.append(toAppend + newline);
        dispatchQueueAreaGui.setCaretPosition(dispatchQueueAreaGui.getDocument().getLength());
    }

    public static void actualizarPeticionesDiscoGui(String toAppend){
        peticionesDiscoAreaGui.append(toAppend + newline);
        peticionesDiscoAreaGui.setCaretPosition(peticionesDiscoAreaGui.getDocument().getLength());
    }

    public static void actualizarResumenGui(String toAppend){
        resumenAreaGui.append(toAppend + newline);
        resumenAreaGui.setCaretPosition(resumenAreaGui.getDocument().getLength());
    }


/*
 MAIN
 */
    public static void main(String args[]) throws InterruptedException{

        Temporizador.tiempoSleep = Integer.parseInt(args[1]);

        proceso.XMLParser xmlParser = new XMLParser(args[0]);
        
        /*
         Interfaz
        */
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });        

        SynchronousQueue<CFQRequest> sq = new SynchronousQueue();

        proceso.DispatchPeticiones dispatchPeticiones = new DispatchPeticiones(
                xmlParser.getListaProcesos(),sq);
        CFQ cfq = new CFQ(40, 20, xmlParser, sq);

        int numPistas = xmlParser.getNumPistas();
        int numBloquesPorPista = xmlParser.getNumBloquesPorPista();

//        CFQRequest a = new CFQRequest(1,1,1,10);
//        CFQRequest b = new CFQRequest(2,0,1,3);
//        CFQRequest c = new CFQRequest(3,1,1,4);
//        CFQRequest d = new CFQRequest(2,0,1,7);
//        CFQRequest e = new CFQRequest(3,0,1,11);
//        CFQRequest f = new CFQRequest(1,1,1,1);
//
////        int rpm = 5400;
////        int numPistas = 4;
////        int numBloquesPorPista = 3;
//
//        dispatchQueue.put(a);
//        dispatchQueue.put(b);
//        dispatchQueue.put(c);
//        dispatchQueue.put(d);
//        dispatchQueue.put(e);
//        dispatchQueue.put(f);
        
        CFQDispatch cfqDispatch = new CFQDispatch(cfq.getCfqQueueAsin(),
            cfq.getCfqQueueSin());

        Disco disco = new Disco(numPistas, numBloquesPorPista);
        disco.run();
        
        

        
    }
}
